/**
 * 系统管理--用户管理的单例对象
 */
var JhcbProduct = {
    id: "jhcbProductTable",//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    product_category_id:0
};

/**
 * 初始化表格的列
 */
JhcbProduct.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '产品名', field: 'title', align: 'center', valign: 'middle', sortable: true},
        {title: '英文名', field: 'english_name', align: 'center', valign: 'middle', sortable: true},
        {title: '创建时间', field: 'create_time', align: 'center', valign: 'middle', sortable: true},
        {title: '状态', field: 'enable', align: 'center', valign: 'middle', sortable: true,
            formatter:function (value, row, index) {
                if (value == '1') return '启用';
                return '未启用';
            }
        }];
    return columns;
};

/**
 * 检查是否选中
 */
JhcbProduct.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        JhcbProduct.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加产品
 */
JhcbProduct.openAddProduct = function () {
    var index = layer.open({
        type: 2,
        title: '添加产品',
        area: ['800px', '560px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/portal/product/product_add'
    });
    this.layerIndex = index;
};

/**
 * 点击修改按钮时
 */
JhcbProduct.openChangeProduct = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '编辑产品',
            area: ['800px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/portal/product/product_edit/' + this.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除产品
 */
JhcbProduct.delJhcbProduct = function () {
    if (this.check()) {

        var operation = function(){
            var productId = JhcbProduct.seItem.id;
            var ajax = new $ax(Feng.ctxPath + "/portal/product/delete", function () {
                Feng.success("删除成功!");
                JhcbProduct.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("productId", productId);
            ajax.start();
        };

        Feng.confirm("是否删除产品" + JhcbProduct.seItem.title + "?",operation);
    }
};

JhcbProduct.resetSearch = function () {
    $("#title").val("");
    $("#beginTime").val("");
    $("#endTime").val("");

    JhcbProduct.search();
}

JhcbProduct.search = function () {
    var queryData = {};

    queryData['product_category_id'] = JhcbProduct.product_category_id;
    queryData['title'] = $("#title").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();

    JhcbProduct.table.refresh({query: queryData});
}

JhcbProduct.onClickProduct = function (e, treeId, treeNode) {
    JhcbProduct.product_category_id = treeNode.id;
    JhcbProduct.search();
};

$(function () {
    var defaultColunms = JhcbProduct.initColumn();
    var table = new BSTable("jhcbProductTable", "/portal/product/list", defaultColunms);
    table.setPaginationType("client");
    JhcbProduct.table = table.init();
    var ztree = new $ZTree("cateGoryTree", "/portal/product/category/tree");
    ztree.bindOnClick(JhcbProduct.onClickProduct);
    ztree.init();
});
